﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcControlToolkit
{
    public static class RoundedCornersExtensions
    {
        private static readonly BoxCorners DEFAULT_CORNERS = BoxCorners.All;
        private static readonly int DEFAULT_RADIUS = 5;

        public static string RoundedCorners(this AjaxHelper helper, string elementId)
        {
            return helper.RoundedCorners(elementId, DEFAULT_RADIUS, DEFAULT_CORNERS);
        }

        public static string RoundedCorners(this AjaxHelper helper, string elementId, BoxCorners corners)
        {
            return helper.RoundedCorners(elementId, DEFAULT_RADIUS, corners);
        }

        public static string RoundedCorners(this AjaxHelper helper, string elementId, int radius)
        {
            return helper.RoundedCorners(elementId, radius, DEFAULT_CORNERS);
        }

        public static string RoundedCorners(this AjaxHelper helper, string elementId, int radius, BoxCorners corners)
        {
            RouteValueDictionary props = new RouteValueDictionary();
            props.AddIfNotEqual("Radius", radius, DEFAULT_RADIUS);
            props.AddIfNotEqual("Corners", corners, DEFAULT_CORNERS);

            return helper.RoundedCorners(elementId, props);
        }

        public static string RoundedCorners(this AjaxHelper helper, string elementId, object properties)
        {
            var sb = new StringBuilder();

            // Add Microsoft Ajax library   
            sb.Append(helper.MicrosoftAjaxInclude());

            /// <reference name="MicrosoftAjax.debug.js" />
            /// <reference name="MicrosoftAjaxTimer.debug.js" />
            /// <reference name="MicrosoftAjaxWebForms.debug.js" />
            /// <reference path="../ExtenderBase/BaseScripts.js" />
            /// <reference path="../Common/Common.js" />

            // Add toolkit scripts   
            sb.Append(helper.ToolkitInclude(
                    "AjaxControlToolkit.ExtenderBase.BaseScripts.js",
                    "AjaxControlToolkit.Common.Common.js",
                    "AjaxControlToolkit.RoundedCorners.RoundedCornersBehavior.js"
                ));

            // Perform $create               
            sb.AppendLine(helper.Create("AjaxControlToolkit.RoundedCornersBehavior", properties, elementId));

            return sb.ToString();
        }
    }

    #region [Enumerations]

    [Flags]
    public enum BoxCorners
    {
        None = 0x00,

        TopLeft = 0x01,
        TopRight = 0x02,
        BottomRight = 0x04,
        BottomLeft = 0x08,

        Top = TopLeft | TopRight,
        Right = TopRight | BottomRight,
        Bottom = BottomRight | BottomLeft,
        Left = BottomLeft | TopLeft,
        All = TopLeft | TopRight | BottomRight | BottomLeft
    }

    #endregion
}
